package com.dimanche.library_base.utils;

import android.util.Log;


import com.dimanche.library_base.constant.Constant;
import com.hjq.toast.ToastUtils;

import java.util.List;

/**
 * author : Dimanche
 * date   : 2020/7/30 10:31
 * desc   :
 * version: 1.0
 */
public class LogUtils {
    private static final String TAG = "LogUtils";


    public static void enter() {
        writeFile("\n");
    }

    /**
     * logi级别的日志
     *
     * @param tag
     * @param msg
     */
    public static void logi(String tag, Object msg) {
        if (msg == null) {
            msg = "null";
        }
        if (Constant.OPEN_LOG && Constant.OPEN_LOG_I) {
            Log.i(tag, "logi: " + msg.toString());
        }
        if (Constant.WRITE_LOG && Constant.WRITE_LOG_I) {
            writeLog(tag + " logi:" + msg.toString());
        }
    }

    /**
     * Loge级别的日志
     *
     * @param tag
     * @param msg
     */
    public static void loge(String tag, Object msg) {
        if (msg == null) {
            msg = "null";
        }
        if (Constant.OPEN_LOG && Constant.OPEN_LOG_E) {
            Log.e(tag, "loge: " + msg.toString());
        }
        if (Constant.WRITE_LOG && Constant.WRITE_LOG_E) {
            writeLog(tag + " loge:" + msg.toString());
        }
    }

    public static void loge(String tag, Object msg, Throwable e) {
        if (msg == null) {
            msg = "null";
        }
        if (Constant.OPEN_LOG && Constant.OPEN_LOG_E) {
            Log.e(tag, "loge: " + msg.toString(), e);
        }
        if (Constant.WRITE_LOG && Constant.WRITE_LOG_E) {
            writeLog(tag + " loge:" + msg.toString() + " " + e.getMessage());
        }
    }

    /**
     * 打印业务日志
     *
     * @param tag
     * @param msg
     */
    public static void logCus(String tag, String type, String scanType, String barCode, Object msg) {
        if (msg == null) {
            msg = "null";
        }
        if (Constant.OPEN_LOG && Constant.OPEN_LOG_I) {
            Log.i(tag, "logCus: " + type + "," + scanType + "," + barCode + "," + msg.toString());
        }
        if (Constant.WRITE_LOG && Constant.WRITE_CUS_LOG) {
            writeLog(tag + " logCus:" + type + "," + scanType + "," + barCode + "," + msg.toString());
        }
    }

    public static void logCus(String tag, Object msg) {
        if (msg == null) {
            msg = "null";
        }
        if (Constant.OPEN_LOG && Constant.OPEN_LOG_I) {
            Log.i(tag, "logCus: " + msg.toString());
        }
        if (Constant.WRITE_LOG && Constant.WRITE_CUS_LOG) {
            writeLog(tag + " logCus:" + msg.toString());
        }
    }


    /**
     * 写日志到文件中
     *
     * @param content
     */
    private static void writeLog(String content) {
        content = TimeUtils.getNowSysTimeOfYMDHMS() + " " + content;
        writeFile(content);
    }

    /**
     * 写日志到文件中
     * 写入规则：
     * 1、以当天日期为文件名称创建文件夹
     * 2、每个文件日志的大小不超过10m，超过则新建一个文件
     *
     * @param content 内容
     */
    private static void writeFile(String content) {
        try {
            //获取今天的日期
            String pathDir = Constant.logPath + TimeUtils.getNowTimeOfYMDCh();
            FileUtils.creatFolder(pathDir);
            String fileDir = "";
            //获取该文件夹中的一级文件数量
            int fileCount = FileUtils.getFolderSize(pathDir);
            if (fileCount == 0) {
                fileCount = 1;
            }
            String fileDirTemp = pathDir + "/" + TimeUtils.getNowTimeOfYMDCh() + "_" + (fileCount) + ".txt";
            //判断日志文件是否超过设定大小
            if (FileUtils.getFileSize(fileDirTemp) > Constant.logFileSize) {
                fileDir = pathDir + "/" + TimeUtils.getNowTimeOfYMDCh() + "_" + (fileCount + 1) + ".txt";
            } else {
                fileDir = fileDirTemp;
            }
            FileUtils.writeString(fileDir, content);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "writeLog: ", e);
            ToastUtils.show(e.getMessage());
        }
    }

    /**
     * 删除无效的log日志文件
     * 删除规则：
     * 1、删除正常写日志的7天之前的log文件，例如1、3、5、7、8、9，其中2、4、6这三天没有写日志，
     * 则不算正常写日志的一天，所以在9号这天仍然不删除1号的日志
     * 2、根据log文件夹下的直接文件夹数量判断，数量大于7时，则认为有应该被删除的日志
     */
    public static void deleteLog() {
        ThreadPoolUtils.executorTask(new Runnable() {
            @Override
            public void run() {
                if (FileUtils.getFolderSize(Constant.logPath) > Constant.LOG_SAVE_DAY) {
                    List<String> folderList = FileUtils.getFileListOrderName(Constant.logPath);
                    for (int i = 0; i < folderList.size() - 7; i++) {
                        FileUtils.deleteFileDir(Constant.logPath + folderList.get(i));
                    }
                }
            }
        });

    }

    /**
     * 上传日志文件至服务器
     */
    public static void uploadLog() {

    }


}
